package com.qyhl.cloud.webtv.module_integral.wallet;

import android.text.TextUtils;

import com.qyhl.webtv.basiclib.utils.DESedeUtil;
import com.qyhl.webtv.basiclib.utils.NetUtil;
import com.qyhl.webtv.basiclib.utils.network.EasyHttp;
import com.qyhl.webtv.basiclib.utils.network.callback.SimpleCallBack;
import com.qyhl.webtv.basiclib.utils.network.exception.ApiException;
import com.qyhl.webtv.commonlib.common.CommonUtils;
import com.qyhl.webtv.commonlib.constant.PathConfigConstant;
import com.qyhl.webtv.commonlib.entity.intergral.ExchangerRateBean;
import com.qyhl.webtv.commonlib.entity.intergral.WallerBean;
import com.qyhl.cloud.webtv.module_integral.app.IntegralApplication;
import com.qyhl.cloud.webtv.module_integral.common.IntergralUrl;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.Map;

public class WalletModel implements WallerContract.WallerModel {
    private WallerContract.WallerPresenter presenter;
    private final String tag = "Wallet";

    public WalletModel(WallerContract.WallerPresenter presenter) {
        this.presenter = presenter;
    }

    @Override
    public void getData() {
        if (NetUtil.isNetworkAvalible(IntegralApplication.getApp())) {
            Map<String, String> map = DESedeUtil.encodeMapUrl(IntergralUrl.INTEGRAL_URL);
            JSONObject jsonObject = new JSONObject();
            String userName = CommonUtils.getInstance().getUserName();
            try {
                jsonObject.put("name", "myCoin");
                jsonObject.put("version", PathConfigConstant.version);
                jsonObject.put("username", userName);
                jsonObject.put("siteId", CommonUtils.getInstance().getSite());
            } catch (JSONException e) {
                e.printStackTrace();
            }

            
            EasyHttp.post(map.get(DESedeUtil.URL))
                    .isDecrypt(true)
                    .decryptKet(map.get(DESedeUtil.CODEKEY))
                    .upBytes(DESedeUtil.encryptMode(map.get(DESedeUtil.CODEKEY), jsonObject.toString()))
                    .execute(new SimpleCallBack<WallerBean>() {

                        @Override
                        public void onError(ApiException e) {
                            presenter.getDataError(TextUtils.isEmpty(e.getMessage()) ? "请求失败" : e.getMessage());
                        }

                        @Override
                        public void onSuccess(WallerBean wallerBean) {
                            presenter.getDataSuccess(wallerBean);
                        }
                    });
        } else {
            presenter.getDataError( "无网络连接，请检查您的网络...");
        }
    }

    @Override
    public void onDestroy() {
        presenter = null;
    }

    @Override
    public void getCoinRate() {
        Map<String, String> map = DESedeUtil.encodeMapUrl(IntergralUrl.INTEGRAL_URL);
        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("name", "coinRate");
            jsonObject.put("version", PathConfigConstant.version);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        EasyHttp.post(map.get(DESedeUtil.URL))
                .upBytes(DESedeUtil.encryptMode(map.get(DESedeUtil.CODEKEY),
                        jsonObject.toString()
                ))
                .isDecrypt(true)
                .decryptKet(map.get(DESedeUtil.CODEKEY))
                .execute(new SimpleCallBack<ExchangerRateBean>() {

                    @Override
                    public void onError(ApiException e) {

                    }

                    @Override
                    public void onSuccess(ExchangerRateBean exchangerRateBean) {
                        presenter.getCoinRateSuccess(exchangerRateBean.getRate());
                    }
                });
        
    }
}
